package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.OrderLine;
import com.zyd.shiro.business.service.PakOrderLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakOrderLine;
import com.zyd.shiro.persistence.mapper.PakOrderLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakOrderLineServiceImpl implements PakOrderLineService {

    @Autowired
    private PakOrderLineMapper pakOrderLineMapper;

    @Override
    public OrderLine insert(OrderLine orderLine) {
        Assert.notNull(orderLine, "对象不可为空！");
        orderLine.getPakOrderLine().setUpdateTime(new Date());
        orderLine.getPakOrderLine().setCreateTime(new Date());
        pakOrderLineMapper.insertSelective(orderLine.getPakOrderLine());
        return orderLine;
    }

    @Override
    public void insertList(List<OrderLine> orderLines) {
        for(OrderLine orderLine:orderLines){
            orderLine.getPakOrderLine().setUpdateTime(new Date());
            orderLine.getPakOrderLine().setCreateTime(new Date());
            pakOrderLineMapper.insertSelective(orderLine.getPakOrderLine());
        }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakOrderLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(OrderLine orderLine) {
        Assert.notNull(orderLine, "对象不可为空！");
        orderLine.setUpdateTime(new Date());
        return pakOrderLineMapper.updateByPrimaryKey(orderLine.getPakOrderLine()) > 0;
    }

    @Override
    public boolean updateSelective(OrderLine orderLine) {
        Assert.notNull(orderLine, "对象不可为空！");
        orderLine.setUpdateTime(new Date());
        return pakOrderLineMapper.updateByPrimaryKeySelective(orderLine.getPakOrderLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public OrderLine getByPrimaryKey(Long primaryKey) {
        PakOrderLine pakOrderLine = pakOrderLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakOrderLine ? null : new OrderLine(pakOrderLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public OrderLine getOneByEntity(OrderLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakOrderLine pakOrderLine = pakOrderLineMapper.selectOne(test.getPakOrderLine());
        return null == pakOrderLine ? null : new OrderLine(pakOrderLine);
    }

    @Override
    public List<OrderLine> listAll() {
        List<PakOrderLine> pakOrderLines = pakOrderLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakOrderLines)) {
            return null;
        }
        List<OrderLine> orderLines = new ArrayList<>();
        for (PakOrderLine pakOrderLine : pakOrderLines) {
            orderLines.add(new OrderLine(pakOrderLine));
        }
        return orderLines;
    }

    @Override
    public List<OrderLine> listByEntity(OrderLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<OrderLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakOrderLine> pakOrderLines = pakOrderLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakOrderLines)) {
            return null;
        }
        List<OrderLine> orderLines = new ArrayList<>();
        for (PakOrderLine pakOrderLine : pakOrderLines) {
            orderLines.add(new OrderLine(pakOrderLine));
        }
        PageInfo bean = new PageInfo<PakOrderLine>(pakOrderLines);
        bean.setList(orderLines);
        return bean;
    }
@Override
    public void increase(PakOrderLine pakOrderLine) {
        pakOrderLineMapper.insert(pakOrderLine);
    }

    @Override
    public List<PakOrderLine> findByOrder(Long orderId) {
        return pakOrderLineMapper.findByOrder(orderId);
    }


}
